package com.livegoods.config;

import com.livegoods.message.LivegoodsMessagePublisher;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class BuyactionConfig {

    /**
     * Redis中缓存的数据是 JSON 格式
     *
     * 默认RedisTemplate去Redis获取数据并解析数据是使用二进制
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }


    @Bean
    public LivegoodsMessagePublisher livegoodsMessagePublisher(AmqpTemplate amqpTemplate){

        LivegoodsMessagePublisher livegoodsMessagePublisher = new LivegoodsMessagePublisher();
        livegoodsMessagePublisher.setAmqpTemplate(amqpTemplate);

        return livegoodsMessagePublisher;
    }

}
